import {EventEmitter} from 'events'
import {Config,parseInput} from './config'
import {TextSelectionHandler} from './event/textSelectionHandler'
import {generatorChildToElement} from './infrastructure/Entity/element'
export class Annotation extends EventEmitter{
    readonly data:Config;
    readonly element:HTMLElement;
    readonly split:RegExp|null|undefined
    readonly textSelectionHandler: TextSelectionHandler
    readonly paragraphArr: Array<string>

    constructor(data:Config,element:HTMLElement,split?:RegExp|undefined|null){
        super()
        // user upload an data to me then make this data right
        const config = parseInput(data||{})
        this.data = data;
        this.paragraphArr = data.content.split("\n")
        this.element = element;
        this.split = split;
        this.textSelectionHandler = new TextSelectionHandler(this,config)
        this.generatorElementToContainer(this.data.content)
        this.registerEventHandlers()
    }
    private generatorElementToContainer(content:string){
      //init these span to container
      content.split("\n").forEach(it=>{
        if(it === ""){
            generatorChildToElement(this.element,'br')
        }else{
            generatorChildToElement(this.element,'span',it)
        }
    })
    }
    private registerEventHandlers(){
        this.element.onmouseup = (e)=>{
            this.textSelectionHandler.textSelection()
        }
    }
    public updateParagraphArr(){
        console.log(this.element.children)
    }
}
